package 蓝桥._2024.植物生命力;

import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {

    static class Node{
        int v;
        Node parent;

        public Node(int v, Node parent) {
            this.v = v;
            this.parent = parent;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //在此输入您的代码...
        int n = scan.nextInt();
        int s = scan.nextInt() - 1;
        Node[] nodes = new Node[n];
        for (int i = 0; i < n; i++) {
            nodes[i] = new Node(scan.nextInt(), null);
        }
        nodes[s].parent = null;
        HashSet<int[]> list = new HashSet<>();
        for (int i = 0; i < n-1; i++) {
            int a = scan.nextInt();
            int b = scan.nextInt();
            list.add(new int[]{a-1,b-1});
        }
        // 构建树，从根节点开始
        Queue<Integer> queue = new LinkedList<>();
        queue.add(s);
        while (!queue.isEmpty()){
            int p = queue.poll();
            List<int[]> removeList = new ArrayList<>();
            for(int[] arr : list){
                if(arr[0] == p) {
                    nodes[arr[1]].parent = nodes[p];
                    queue.add(arr[1]);
                    removeList.add(arr);
                }else if(arr[1] == p) {
                    nodes[arr[0]].parent = nodes[p];
                    queue.add(arr[0]);
                    removeList.add(arr);
                }
            }
            for (int[] ints : removeList) {
                list.remove(ints);
            }
        }
//        for (int i = 0; i <n;i++){
//            Node node = nodes[i];
//            int v = node.v;
//            node = node.parent;
//            System.out.printf("%d",v);
//            while(node != null){
//                System.out.printf("->%d",node.v);
//                node = node.parent;
//            }
//            System.out.println();
//        }


        int total = 0;
        for (int i = 0; i < n; i++) {
            Node node = nodes[i];
            int v = node.v;
            node = node.parent;
            while(node != null){
                if(v < node.v && node.v %v !=0)
                    total++;
                node = node.parent;
            }
        }
        System.out.println(total);
        scan.close();
    }
}